package cn.js.ccit.servlet;

import cn.js.ccit.entity.Branch;
import cn.js.ccit.entity.Department;
import cn.js.ccit.service.BranchService;
import cn.js.ccit.service.DepartmentService;
import cn.js.ccit.service.impl.BranchServiceImpl;
import cn.js.ccit.service.impl.DepartmentServiceImpl;
import cn.js.ccit.vo.DepartmentVo;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/departmentServlet")
public class DepartmentServlet extends HttpServlet {
    DepartmentService departmentService = new DepartmentServiceImpl();

    BranchService branchService = new BranchServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String departIdStr = req.getParameter("departId");

        if(StringUtils.isNullOrEmpty(departIdStr)){
            //查询部门信息列表
            Department department = new Department();
            List<DepartmentVo> departmentList = departmentService.getAllDepartment();
            req.setAttribute("departmentList", departmentList);
        }else{
            //根据ID查询部门信息
            Integer departId = Integer.parseInt(departIdStr);
            DepartmentVo department = departmentService.getDepartmentById(departId);

            // 查询机构列表
            List<Branch> branchList = branchService.getBranchBy(new Branch());
            Map<String,Object> departAndBranchMap = new HashMap<String,Object>();
            departAndBranchMap.put("department", department);
            departAndBranchMap.put("branchList", branchList);

            req.setAttribute("departAndBranchMap", departAndBranchMap);
            req.getRequestDispatcher("editDepartment.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        // 获取参数
        String departName = req.getParameter("departName");

        // 查看该部门是否存在
        Department department = new Department();
        department.setDepartName(departName);
        List<DepartmentVo> departmentVos = departmentService.getDepartmentBy(department);
        if(departmentVos!=null && departmentVos.size()>0){
            // 部门信息已存在
        }else {
            // 添加部门信息
            String branchIdStr = req.getParameter("branchId");
            String tel = req.getParameter("tel");
            String phone = req.getParameter("phone");
            String fax = req.getParameter("fax");
            String remark = req.getParameter("remark");

            department.setBranchId(Integer.parseInt(branchIdStr));
            department.setTel(tel);
            department.setPhone(phone);
            department.setFax(fax);
            department.setRemark(remark);
            departmentService.addDepartment(department);
        }
        resp.sendRedirect("departmentList.jsp");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String departIdStr = req.getParameter("departId");

        if(!StringUtils.isNullOrEmpty(departIdStr)){
            //根据ID删除部门信息
            Integer departId = Integer.parseInt(departIdStr);
            departmentService.deleteDepartmentById(departId);
        }
        resp.sendRedirect("departmentList.jsp");
    }
}
